<template>
  <div class="pageVerify">
    <div class="loading">
      <div class="la-ball-atom la-dark la-2x">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
      <div class="helpText">加载中...</div>
    </div>
  </div>
</template>
<script setup>
import { nextTick, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getUrlParam } from "@/utils/help";
import { useStore } from "vuex";
import {
  WX_LOGIN,
  getDingTalkCode,
  DingTalk_LOGIN,
  QYWX_LOGIN,
  getDingTalk_config,
  APP_LOGIN,
} from "@/utils/login";

const router = useRouter();
const route = useRoute();
const store = useStore();

let originUrl = window.location.href;

let DingT = sessionStorage.getItem("DingT") || "";
let isToken = sessionStorage.getItem("token");
let VerifyConfig = {};

watch(
  () => store?.state?.config?.config,
  () => {
    nextTick(() => {
      if (!sessionStorage.getItem("pageConfig")) {
        store.dispatch("updateConfig");
        return false;
      }
      VerifyConfig = JSON.parse(sessionStorage.getItem("pageConfig"));
      filterDev();
    });
  },
  {
    immediate: true,
  }
);

async function filterDev() {
  console.log("VerifyConfig", VerifyConfig);
  if (agent == "APP") {
    let token = sessionStorage.getItem("loginToken");
    token && APP_LOGIN({ token });
  } else if (agent == "DEFAULT" || isToken) {
    router.replace("/home");
  } else if (agent == "WX" && VerifyConfig?.wechat?.app_id) {
    let code = route.query.code || "";
    let openId = localStorage.getItem("openId") || "";
    if (!code && !openId) {
      let redirectUrl = `https://open.weixin.qq.com/connect/oauth2/authorize`;
      let params = {
        appid: VerifyConfig?.wechat?.app_id,
        redirect_uri: encodeURIComponent(originUrl),
        response_type: "code",
        scope: "snsapi_base",
      };

      window.location.href = `${redirectUrl}?appid=${params.appid}&redirect_uri=${params.redirect_uri}&response_type=${params.response_type}&scope=${params.scope}&state=1&connect_redirect=1#wechat_redirect`;
    } else {
      console.log("我是微信授权code", code);
      if (code) {
        WX_LOGIN({ code });
      } else if (openId) {
        WX_LOGIN({ openId });
      }
    }
  } else if (agent == "QY_WX" && VerifyConfig?.wxwork?.app_id) {
    let code = route.query.code || "";
    // let openId = localStorage.getItem("openId") || "";
    if (!code) {
      let redirectUrl = `https://open.weixin.qq.com/connect/oauth2/authorize`;
      let params = {
        appid: VerifyConfig?.wxwork?.app_id,
        redirect_uri: encodeURIComponent(originUrl),
        response_type: "code",
        scope: "snsapi_base",
      };

      window.location.href = `${redirectUrl}?appid=${params.appid}&redirect_uri=${params.redirect_uri}&response_type=${params.response_type}&scope=${params.scope}&state=1&connect_redirect=1#wechat_redirect`;
    } else {
      console.log("企业微信-------------------");
      console.log(window.location.href);

      QYWX_LOGIN({ code });
    }
  } else if (agent == "DingTalk" && VerifyConfig?.dingtalk?.app_key) {
    let dingCode = await getDingTalkCode(VerifyConfig?.dingtalk?.corp_id);
    console.log("我获取了钉钉企业id，使用dd.ready获取了授权code", dingCode);
    let code = dingCode || route.query.code || "";

    if (!code) {
      let redirectUrl = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize`;
      // let corpInfo = await getDingTalk_config();
      // console.log("钉钉企业ID -------- ", corpInfo);
      // if (!corpInfo.app_key) return false;
      let params = {
        appid: VerifyConfig?.dingtalk?.app_key,
        redirect_uri: encodeURIComponent(originUrl),
        response_type: "code",
        scope: "snsapi_auth",
      };
      console.log("钉钉企业ID -------- ", JSON.stringify(params));
      console.log(
        "钉钉跳转地址 -------- ",
        `${redirectUrl}?redirect_uri=${params.redirect_uri}&response_type=${params.response_type}&appid=${params.appid}&scope=${params.scope}&state=1`
      );
      // location.hash = "#/login";
      setTimeout(() => {
        location.href = `${redirectUrl}?redirect_uri=${params.redirect_uri}&response_type=${params.response_type}&appid=${params.appid}&scope=${params.scope}&state=1`;
      }, 0);
    } else {
      console.log("钉钉-------------------");
      console.log(window.location.href);

      DingTalk_LOGIN({ code });
    }
  } else {
    sessionStorage.setItem("authError", true);
    router.replace("/home");
  }
}
</script>
<style lang="scss" scoped>
.pageVerify {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #00164b;
}
.helpText {
  margin-top: 45px;
  position: relative;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
}
.loading {
  margin: 20px auto;
  position: relative;
  top: -10%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>
